package com.shiro.test.mvc.controller;

import com.shiro.test.mvc.pojo.User;
import com.shiro.test.mvc.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2020/11/23 0023.
 */
@Controller
public class LoginController {

    @Autowired
    private IUserService userService;

    @RequestMapping("gologin.html")
    public String goLogin(){
        return "login";
    }
    @RequestMapping("login.html")
    public String login(String email, String password, HttpServletRequest request){
        User user = userService.doLogin(email, password);
        if(user == null){
            request.setAttribute("error","用户名或密码错误");
            return "login";
        }
        return "redirect:index.html";
    }

    @RequestMapping("logout.html")
    public String logout(String username, String password, HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:gologin.html";
    }
}
